<?php
/**
 * Created by PhpStorm.
 * User: WenQing
 * Date: 2017/7/24
 * Time: 10:10
 */

if($_POST)
{
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
    $redis->incr('id');
    $id = $redis->get('id');

    //判断用户名是否存在,存在了不允许添加
    $allUser = $redis->keys('user*');
    $rename = 0; //默认没有重名
    foreach ($allUser as $v)
    {
        $temp = $redis->hGet($v,'username');
        if($_POST['username'] == $temp) $rename = 1;
    }

    if(empty($rename))
    {
        $redis->lPush('user','user'.$id); //这个是分页使用
        $redis->hMset('user'.$id,[
            'username' => $_POST['username'],
            'age' => $_POST['age'],
            'sex' => $_POST['sex']
        ]);
    }

    header('Location: http://www.php_redis.com/demo/index.php');
}